package com.itheima.controller;

import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {


    @Autowired
    private EmpService empService;


    //2.1 员工列表查询
    @GetMapping
    public Result queryEmployeeList(EmpQueryParam empQueryParam) {

        log.info("请求参数,{}", empQueryParam);
        PageResult pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    //2.2删除员工
    @DeleteMapping
    public Result deleteEmployee(@RequestParam List<Integer> ids) {
        log.info("删除员工,ids {}", ids);
        empService.deleteEmployee(ids);
        return Result.success();
    }

    //添加员工
    @PostMapping
    public Result addEmployee(@RequestBody Emp emp) {
        log.info("请求参数emp: {}", emp);
        empService.addEmployee(emp);
        return Result.success();
    }

    //.4 根据ID查询
    @GetMapping("/{id}")
    public Result queryById(@PathVariable("id") String id) {
        try {
            Integer empId = Integer.valueOf(id);
            Emp emp = empService.findById(empId);
            return Result.success(emp);
        } catch (NumberFormatException e) {
            return Result.error("员工ID必须是数字");
        }
    }

    //修改员工
    @PutMapping
    public Result modifyEmployee(@RequestBody Emp emp) {
        log.info("修改员工emp: {}", emp);
        empService.modifyEmployee(emp);
        return Result.success();
    }

    //2.6 查询全部员工
    @GetMapping("/list")
    public Result queryAllEmployees() {
        List<Emp> list = empService.queryAllEmployees();
        return Result.success(list);
    }
}